package com.sm.controller;

import com.sm.annotation.IgnoreAuth;
import com.sm.model.AppConfig;
import com.sm.service.AppConfigService;
import com.sm.service.ShareImgService;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/appConfig")
public class AppConfigController {

    @Autowired
    private AppConfigService appConfigService;

    @Autowired
    private ShareImgService shareImgService;

    @IgnoreAuth
    @RequestMapping("/verifyAppVersion")
    @ResponseBody
    public Msg verifyAppVersion(@RequestParam("version") String version, HttpServletResponse response) throws IOException {
        Map<String, Object> result = new HashMap<>();
        String appVersion = appConfigService.getByKey("app_version").getValue();
        String appUrl = appConfigService.getByKey("app_url").getValue();
        String appUpgradeContent = appConfigService.getByKey("app_upgrade_content").getValue();
        //0代表版本一致不用更新
        int status = 0;
        if (appVersion.equals(version)) {
            response.sendError(500);
            result.put("status", status);
            return Msg.success(result);
        } else {
            //1代表版本不一致需要更新
            status = 1;
            result.put("status", status);
            result.put("appVersion", appVersion);
            result.put("appUrl", appUrl);
            result.put("appUpgradeContent", appUpgradeContent);
            return Msg.success(result);
        }
    }

    @IgnoreAuth
    @RequestMapping("/showVip")
    @ResponseBody
    public Msg showVip() {
        Map<String, Object> result = new HashMap<>();
        String showVip = appConfigService.getByKey("show_vip").getValue();
        result.put("showVip", showVip);
        return Msg.success(result);
    }

    @IgnoreAuth
    @RequestMapping("getShareImg")
    @ResponseBody
    public Msg getShareImg() {
        return Msg.success(shareImgService.getCurrentImg());
    }
}
